package cn.seaboot.admin.goal;

/**
 * 逻辑关系
 *
 * @author Mr.css
 * @version 2022-11-26 17:32
 */
public enum Logical {
    /**
     * 与
     * <br/>1 AND 1 = 1
     * <br/>1 AND 0 = 0
     * <br/>0 AND 1 = 0
     * <br/>0 AND 0 = 0
     */
    AND(0),
    /**
     * 或
     * <br/>1 OR 1 = 1
     * <br/>1 OR 0 = 1
     * <br/>0 OR 1 = 1
     * <br/>0 OR 0 = 0
     */
    OR(1),
    /**
     * 非
     * <br/>NOT 1 = 0
     * <br/>NOT 0 = 1
     */
    NOT(2),
    /**
     * 异或，不同则为真
     * <br/> 1 XOR 1 = 0
     * <br/> 1 XOR 0 = 1
     * <br/> 0 XOR 1 = 1
     * <br/> 0 XOR 0 = 0
     */
    XOR(3),
    /**
     * 或非，相同则为真
     * <br/>1 XNOR 1 = 1
     * <br/>1 XNOR 0 = 0
     * <br/>0 XNOR 1 = 0
     * <br/>0 XNOR 0 = 1
     */
    XNOR(4),
    /**
     * 与非，非与。与的结果取反。
     * 相同为假，其余为真。
     * <br/>1 NAND 1 = 0
     * <br/>1 NAND 0 = 1
     * <br/>0 NAND 1 = 1
     * <br/>0 NAND 0 = 1
     */
    NAND(5),
    /**
     * 或非，非或。或的结果取反
     * 全假为真，其余为真。
     * <br/>1 NOR 1 = 0
     * <br/>1 NOR 0 = 0
     * <br/>0 NOR 1 = 0
     * <br/>0 NOR 0 = 1
     */
    NOR(6);

    private final int value;

    Logical(int value) {
        this.value = value;
    }

    public int value() {
        return this.value;
    }

    public static Logical valueOf(int value) {
        return Logical.values()[value];
    }

    /**
     * 逻辑运算
     *
     * @param a 值 a
     * @param b 值 b
     * @return 逻辑判断结果
     */
    public boolean process(boolean a, boolean b) {
        switch (this) {
            case AND:
                return a && b;
            case OR:
                return a || b;
            case NOT:
                // attention!
                return !a;
            case XOR:
                return a ^ b;
            case XNOR:
                return a == b;
            case NAND:
                return !(a && b);
            case NOR:
                return !(a || b);
            default:
                return false;
        }
    }

    /**
     * 逻辑运算
     *
     * @param a 值 a
     * @param b 值 b
     * @return 逻辑判断结果
     */
    public int process(int a, int b) {
        return process(a > 0, b > 0) ? 1 : 0;
    }
}
